using System.Text;
using LDNS.Utilities;
using LDNS.SDK.Commands.Models;

namespace LDNS.Commands
{
    /// <summary>
    /// 获取配置命令处理器
    /// 处理GETCONFIG命令，获取当前DNS服务的配置信息
    /// 作者：huobing
    /// </summary>
    public class GetConfigCommandHandler : CommandHandlerBase<GetConfigCommand, GetConfigResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public override string CommandName => GetConfigCommand.NAME;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        public GetConfigCommandHandler()
            : base()
        {
        }
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数</param>
        /// <returns>执行结果</returns>
        public override BaseCommandResult Execute(string[] args)
        {
            try
            {
                var config = DnsConfigManager.Instance.Config;
                GetConfigResult result = CreateSuccessResult();
                result.Port = config.Port;
                result.EnableCache = config.EnableCache;
                result.CacheExpirationMinutes = config.CacheExpirationMinutes;
                result.MaxConnections = config.MaxConnections;
                result.EnableSocketManagement = config.EnableSocketManagement;
                result.SocketManagementPort = config.SocketManagementPort;
                result.EnableAccessControl = config.EnableAccessControl;
                result.AllowedIPs = config.AllowedIPs;
                result.DefaultRecordTtl = config.DefaultRecordTtl;
                result.UpstreamDnsServers = config.UpstreamDnsServers;
                return result;
            }
            catch (Exception ex)
            {
                return CreateErrorResult($"获取配置信息失败: {ex.Message}");
            }
        }
    }
}